package com.itheima.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.take_delivery.Order;
import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.service.base.IOrderService;
import com.itheima.bos.service.take_delivery.IWayBillService;
import com.itheima.bos.web.action.base.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * 运单的action
 * @author yanju
 *
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class WayBillAction extends BaseAction<WayBill> {
	
	@Autowired
	private IWayBillService wayBillServiceImpl;
	
	
	//创建日志对象,输出日志
	private static final Logger LOGGER=Logger.getLogger(WayBillAction.class);
	
	@Action(value="waybill_save",results={@Result(name="success",type="json")})
	public String waybill_save(){
		
		//创建map集合返回结果
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			wayBillServiceImpl.save(model);
			map.put("success",true);
			map.put("msg", "保存成功");
			LOGGER.info("保存运单成功,单号为:"+model.getWayBillNum());
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success",false);
			map.put("msg", "保存失败");
			LOGGER.info("保存运单成功,单号为:"+model.getWayBillNum());
			
		}
		ActionContext.getContext().getValueStack().push(map);	
		
		return SUCCESS;
		
		
	}
	
	//查询分页的action
	
	@Action(value="waybill_QueryPage",results={@Result(name="success",type="json")})
	public  String waybill_QueryPage(){
		//创建分页对象
		 
		Pageable pageable = new PageRequest(page-1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		
		Page<WayBill> pagedata=wayBillServiceImpl.findAll(model,pageable);
		
		//返回数据
		getPageValueStack(pagedata);
		return SUCCESS;
	}
	
	//查询出运单号的信息的action
	

	@Action(value="wayBill_findWayBillNum",results={@Result(name="success",type="json")})
	public String wayBill_findAll(){
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		
		WayBill wayBillNumData=wayBillServiceImpl.findByWayBillNum(model.getWayBillNum());
		
		if (wayBillNumData==null) {
			//没查询到运单
			map.put("success", false);
		}else {
			//查出运单数据并封装
			map.put("success", true);
			map.put("wayBillNumData", wayBillNumData);
		}
		
		
		//压入值站
		ActionContext.getContext().getValueStack().push(map);
		
		return SUCCESS;
	}
	
	@Autowired
	private IOrderService orderServcieImpl;
	
	
	//订单号数据回显的action
	@Action(value="order_findByOrderNum",results={@Result(name="success",type="json")})
	public String order_findByOrderNum(){
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		Order order=orderServcieImpl.findByOrderNum(model.getOrder().getOrderNum());
		
		if (order==null) {
			//没有订单
			map.put("success",false);
		}else {
			//查询到订单
			map.put("success", true);
			map.put("orderData",order);
		}
		
		return SUCCESS;
	} 
	
	
	
	
	
	
}
